'''
Created on 8 Nov 2014

@author: User
'''
import copy

from main.data import lead_ind
from main.data import mylog, basic_ind
from main.lib import tech_ind
from main.model.prices import Prices
from main.model.trade import Trade


class SmaStrategy:
    
    def __init__(self):
        self.trades=[];
        self.prices=None;
        self.title= "Buy when the SMA50 crosses over the SMA150.";
        
    def backTest(self,prices):
        self.prices=prices;
        
        sma150 = lead_ind.getSMA(prices.closes, 150);
        sma50 = lead_ind.getSMA(prices.closes, 50);
        
        
        #sma150 = sma150[::-1];
        #sma50 = sma50[::-1];
        #self.prices.reverse();
        
        mylog.info("150 Len: " + str(len(sma150)));
        mylog.info("50 Len: " + str(len(sma50)));
        
        mylog.info("Number of prices: " + str(len(prices.closes)));
        
        for i in range(len(sma150)-2,0,-1):
            #
            
            if sma50[i] > sma150[i]:
                if sma50[i+1] < sma150[i+1]:
                    mylog.info("Opening Trade {0} SMA50:{1} SMA150:{2}".format(self.prices.datestrings[i],sma50[i],sma150[i]));
                    trade = Trade();
                    trade.openPrice = self.prices.closes[i];
                    trade.opentime = self.prices.datestrings[i];
                    trade.stopPrice = self.prices.closes[i] * .9; # Stop Loss 10% lower.
                    trade.targetPrice = self.prices.closes[i] * 1.2; #Target 50% higher.
                    trade.timeLimit = 125; # About 6 Months.
                    trade.pos= i;
                    mylog.debug("Trade Found")
                    self.trades.append(trade);
                
        mylog.info("Number of trades found: " +str(len(self.trades)));
        for trade in self.trades:
            trade.check(self.prices);
            mylog.info("Trade Result: Closed:{1} Price Opened: {2} Closed: {3} Profit/Loss:{4:.2f}% Reason: {5}" .format(
            trade.opentime,trade.closeTime,trade.openPrice,trade.closePrice,trade.getProfitLoss(),trade.endCondition));
    
    def printEffectiveness(self):
        print "Number of days Checked: " + str(len(self.prices.closes));
        print("Date Beginning: " + self.prices.datestrings[0]);
        
        print "Number of trades found total: " + str(len(self.trades));
        print "Trades per year: " + str( len(self.trades) / (len(self.prices.closes)/250.) );
        
        avgProf = 0.;
        
        for trade in self.trades:
            avgProf += trade.getProfitPerDay();
            
        avgProf /= len(self.trades);
        
        print "Average Profit Per Day: {0:.3f}".format(avgProf);
        
        print "Calculating Mean for this instrument...";
        
       
        
        profit = basic_ind.pctGainLoss(self.prices.closes[-1],self.prices.closes[0]);
        profit /= len(self.prices.closes);
        print "Average Profit Per Day for this Instrument: {0:.3f}".format(profit); 
    
           
            